﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.Common;
using Mimoza.Transport;

namespace Mimoza.DBLayer.Transport.Server
{
    public class TransportDBLayer : Mimoza.Common.IDBLayer
    {
        public TransportDBLayer()
        {
            Common.RegistrationRequest.RegisterHandler(RegistrationRequestHandler);
        }

        public void Connect()
        {
        }

        public void ConnectToHostDB()
        { 
        }

        public void SetDBLayer(Mimoza.Common.IDBLayer dbLayer)
        {
            m_dbLayer = dbLayer;
            m_dbContainers = new DBContainersServer(
                m_dbLayer.DBContainerRepository, m_collectionManager, m_connections);
            m_dbHostsRepository = new HostsRepository(
                m_dbLayer.DBHostRepository, m_collectionManager, m_connections);
            m_dbPoliciesRepository = new PoliciesRepository(
                m_dbLayer.DBPolicyRepository, m_collectionManager, m_connections);
            m_dbUpdatesRepository = new UpdatesRepository(
                m_dbLayer.DBUpdateRepository, m_collectionManager, m_connections);
        }

        public void Disconnect()
        {
        }

        public IDBContainerRepository DBContainerRepository
        {
            get
            {
                return m_dbContainers;
            }
        }

        public IDBHostRepository DBHostRepository
        {
            get
            {
                return m_dbHostsRepository;
            }
        }

        public IDBPolicyRepository DBPolicyRepository
        {
            get
            {
                return m_dbPoliciesRepository;
            }
        }

        public IDBUpdateRepository DBUpdateRepository
        {
            get
            {
                return m_dbUpdatesRepository;
            }
        }

        Common.RegistrationReply RegistrationRequestHandler(Common.RegistrationRequest request)
        {
            string remoteAddr = request.RemoteAddress;

            m_connections.Add(request.Connection);

            return new Common.RegistrationReply(request.TransportDbLayerId);
        }

        Mimoza.Common.IDBLayer m_dbLayer = null;

        DBContainersServer m_dbContainers = null;
        HostsRepository m_dbHostsRepository = null;
        PoliciesRepository m_dbPoliciesRepository = null;
        UpdatesRepository m_dbUpdatesRepository = null;

        CollectionManager m_collectionManager = new CollectionManager();

        ConnectionList m_connections = new ConnectionList();
    }
}
